<?php

class Services_IndexController extends App_Controller_Action { 
/**
     * Отображает главную страницу
     */
    public function indexAction() 
    {
    	$modelServices = new Services();
    	$services = $modelServices->getServices();
        $this->view->services = $services;
    }

    /**
     * Страница из меню
     */    
    public function viewAction() 
    {
        $serviceId = $this->_getParam('serviceId');
        $modelServices = new Services();
        $service = $modelServices->getService($serviceId);
        $this->view->service = $service;
    }

    public function adminlistAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$modelServices = new Services();
    	$products = $modelServices->getServices();
        $this->view->services = $products;
    }
    
    public function adminformAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	if ($this->_request->isPost()) {
    		$formService = new FormServices('');
    		if ($formService->isValid($this->_getAllParams())) {
    			$post = $this->_request->getPost();
    			$service = new Services();
    			$service->setDataFromArray($post);
    			if ($service->id) {
	    			$service->update();
	    		} else {
    				$service->insert();
	    		}
	    		$this->_helper->redirector->gotoRoute(array(), 'adminServices');
    		}
			$this->view->formServices = $formService;
    	} else {
	    	$id = $this->_getParam('serviceId');
	        $modelServices = new Services();
	        $service = $modelServices->getService($id);
	        $this->view->formServices = $service;
			$formServices = new FormServices($service);
			$this->view->formServices = $formServices;
    	}
    }

    public function admindeleteAction() {
    	if (!$this->isAuth()) {	$this->_helper->redirector->gotoRoute(array(), 'adminLogin'); }
    	$id = $this->_getParam('productId');
        $modelServices = new Services();
        $res = $modelServices->delete('id='.$id);
		$this->_helper->redirector->gotoRoute(array(), 'adminServices');
    }
}